<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" label-width="120px">
      <el-form-item label="集团" prop="companyId">
        <el-select v-model="dataForm.companyId" placeholder="集团">
          <el-option v-for="item in dict.companyId" :key="item.code" :label="item.name" :value="parseInt(item.code)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="dataForm.account" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="dataForm.userName" placeholder="姓名"></el-input>
      </el-form-item>

      <el-form-item label="账号类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="账号类型" :rules="[{ required: true, message: '请选择分类', trigger: 'blur' }]">
          <el-option v-for="item in dict.type" :key="item.code" :label="item.name" :value="parseInt(item.code)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="状态" :rules="[{ required: true, message: '状态', trigger: 'blur' }]">
          <el-option v-for="item in dict.status" :key="item.code" :label="item.name" :value="parseInt(item.code)"></el-option>
        </el-select>
      </el-form-item>

    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        companyId: "",
        account: "",
        password: "",
        userName: "",
        type: 1,
        status: "",
        dueTime: "",
        email: ""
      },
      dict: {
        status: baseService.strDict("0:停用,1:启用"),
        type: baseService.strDict("1:集团账号,2:集团下属学校账号,3:老师账号,4:学生账号"),
        companyId: [{ code: "", name: "" }]
      }
    });
  },
  computed: {
    dataRule() {
      return {};
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        baseService.urlDict("fii/fiicompany/dict").then((a) => {
          this.dict.companyId = a;
        });
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/fii/fiiuser/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/fii/fiiuser", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
